package com.vegetable.modules.service.finance.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vegetable.common.annotation.SalesDataPermission;
import com.vegetable.modules.entity.finance.FinancialCollectionDetail;
import com.vegetable.modules.entity.finance.po.FinancialCollectionDetailPO;
import com.vegetable.modules.entity.finance.po.FinancialCollectionStatisticsPO;
import com.vegetable.modules.entity.finance.vo.FinancialCollectionStatistics;
import com.vegetable.modules.mapper.finance.FinancialCollectionDetailMapper;
import com.vegetable.modules.service.finance.IFinancialCollectionDetailService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 财务收款管理 服务实现类
 * </p>
 *
 * @author wangruijie
 * @since 2024-04-13
 */
@Slf4j
@Service
@AllArgsConstructor
public class FinancialCollectionDetailServiceImpl extends ServiceImpl<FinancialCollectionDetailMapper, FinancialCollectionDetail> implements IFinancialCollectionDetailService {


    @Override
    public List<FinancialCollectionDetail> findByOrderId(Long orderId) {
        FinancialCollectionDetailPO po = new FinancialCollectionDetailPO();
        po.setOrderId(orderId);
        return this.baseMapper.queryFinancialCollectionList(po);
    }

    @Override
    public List<FinancialCollectionDetail> findByMainId(Long mainId) {
        FinancialCollectionDetailPO po = new FinancialCollectionDetailPO();
        po.setMainId(mainId);
        return this.baseMapper.queryFinancialCollectionList(po);
    }

    @Override
    @SalesDataPermission(type = SalesDataPermission.PermissionType.CUSTOMER, filterParam = FinancialCollectionStatisticsPO.class)
    public Map<String, Object> salesCollectionStatistics(FinancialCollectionStatisticsPO po) {
        Map<String, Object> result = new HashMap<>();
        IPage iPage = new Page(po.getPageCurrent(), po.getPageSize());
        IPage<FinancialCollectionStatistics> page = baseMapper.queryFinancialCollectionListByCustomer(iPage, po);
        IPage<FinancialCollectionStatistics> total = baseMapper.queryFinancialCollectionListByCustomer( new Page(1L, Long.MAX_VALUE), po);
        List<FinancialCollectionStatistics> records = total.getRecords();
        BigDecimal totalReceivedMoney = records.stream().map(FinancialCollectionStatistics::getTotalReceivedMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal totalAllocatedMoney = records.stream().map(FinancialCollectionStatistics::getTotalAllocatedMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal totalUnAllocatedMoney = records.stream().map(FinancialCollectionStatistics::getTotalUnAllocatedMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
        result.put("records", page);
        result.put("totalReceivedMoney", totalReceivedMoney);
        result.put("totalAllocatedMoney", totalAllocatedMoney);
        result.put("totalUnAllocatedMoney", totalUnAllocatedMoney);
        return result;
    }
}